<template>
  <el-main class="bg edit_wrap">
    <el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
      <el-col v-if="user_group === '管理员' || $check_field('get','user') || $check_field('add','user') || $check_field('set','user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="用户" prop="user">
          <div v-if="user_group !== '管理员'">
            {{ get_user_session_user(form['user']) }}
            <!--<el-input id="business_name" v-model="form['user']" placeholder="请输入用户"-->
            <!--v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','user')) || (!form['approval_documents_id'] && $check_field('add','user'))" :disabled="disabledObj['user_isDisabled']"></el-input>-->
            <!--<div v-else-if="$check_field('get','user')">{{form['user']}}</div>-->
            <el-select v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','user')) || (!form['approval_documents_id'] && $check_field('add','user'))" id="user" v-model="form['user']" :disabled="disabledObj['user_isDisabled']">
              <el-option v-for="o in list_user_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
                         :value="o['user_id']">
              </el-option>
            </el-select>
            <el-select v-else-if="$check_field('get','user')" id="user" v-model="form['user']" :disabled="true">
              <el-option v-for="o in list_user_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
                         :value="o['user_id']">
              </el-option>
            </el-select>
          </div>
          <el-select v-else id="user" v-model="form['user']" :disabled="disabledObj['user_isDisabled']">
            <el-option v-for="o in list_user_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
                       :value="o['user_id']">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-if="user_group === '管理员' || $check_field('get','manager') || $check_field('add','manager') || $check_field('set','manager')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="经理" prop="manager">
          <el-select v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','manager')) || (!form['approval_documents_id'] && $check_field('add','manager'))" id="manager" v-model="form['manager']" :disabled="disabledObj['manager_isDisabled']">
            <el-option v-for="o in list_user_manager" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
                       :value="o['user_id']">
            </el-option>
          </el-select>
          <el-select v-else-if="$check_field('get','manager')" id="manager" v-model="form['manager']" :disabled="true">
            <el-option v-for="o in list_user_manager" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
                       :value="o['user_id']">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-if="user_group === '管理员' || $check_field('get','file_name') || $check_field('add','file_name') || $check_field('set','file_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="文件名称" prop="file_name">
          <el-input id="file_name" v-model="form['file_name']" placeholder="请输入文件名称"
                    v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','file_name')) || (!form['approval_documents_id'] && $check_field('add','file_name'))" :disabled="disabledObj['file_name_isDisabled']"></el-input>
          <div v-else-if="$check_field('get','file_name')">{{form['file_name']}}</div>
        </el-form-item>
      </el-col>
      <el-col v-if="user_group === '管理员' || $check_field('get','file_type') || $check_field('add','file_type') || $check_field('set','file_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="文件类型" prop="file_type">
          <el-input id="file_type" v-model="form['file_type']" placeholder="请输入文件类型"
                    v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','file_type')) || (!form['approval_documents_id'] && $check_field('add','file_type'))" :disabled="disabledObj['file_type_isDisabled']"></el-input>
          <div v-else-if="$check_field('get','file_type')">{{form['file_type']}}</div>
        </el-form-item>
      </el-col>
      <el-col v-if="user_group === '管理员' || $check_field('get','treatment_method') || $check_field('add','treatment_method') || $check_field('set','treatment_method')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="处理方式" prop="treatment_method">
          <el-select id="treatment_method" v-model="form['treatment_method']"
                     v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','treatment_method')) || (!form['approval_documents_id'] && $check_field('add','treatment_method'))">
            <el-option v-for="o in list_treatment_method" :key="o" :label="o" :value="o">
            </el-option>
          </el-select>
          <div v-else-if="$check_field('get','treatment_method')">{{form['treatment_method']}}</div>
        </el-form-item>
      </el-col>
      <el-col v-if="user_group === '管理员' || $check_field('get','document_attachment') || $check_field('add','document_attachment') || $check_field('set','document_attachment')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="文件附件" prop="document_attachment">
          <div v-if="disabledObj['document_attachment_isDisabled']">
            <div v-if="$check_field('get','document_attachment')">
              <el-button type="primary" @click="$download($fullUrl(form['document_attachment']),form['document_attachment'])">下载<i
                  class="el-icon-download el-icon--right"></i></el-button>
            </div>
          </div>
          <div v-else>
            <el-upload v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','document_attachment')) || (!form['approval_documents_id'] && $check_field('add','document_attachment'))" class="upload-demo" drag
                       action="" style="max-width: 300px;width: 100%;" :http-request="upload_document_attachment" :limit="1" accept="">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
            <div v-else-if="$check_field('get','document_attachment')">
              <el-button type="primary" @click="$download($fullUrl(form['document_attachment']),form['document_attachment'])">下载<i
                  class="el-icon-download el-icon--right"></i></el-button>
            </div>
          </div>
        </el-form-item>
      </el-col>
      <el-col v-if="user_group === '管理员' || $check_field('get','document_details') || $check_field('add','document_details') || $check_field('set','document_details')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="文件详情" prop="document_details">
          <el-input type="textarea" id="document_details" v-model="form['document_details']" placeholder="请输入文件详情"
                    v-if="user_group === '管理员' || (form['approval_documents_id'] && $check_field('set','document_details')) || (!form['approval_documents_id'] && $check_field('add','document_details'))" :disabled="disabledObj['document_details_isDisabled']"></el-input>
          <div v-else-if="$check_field('get','document_details')">{{form['document_details']}}</div>
        </el-form-item>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="审核状态" prop="examine_state">
          <el-select id="examine_state" v-model="form['examine_state']"
                     v-if="user_group === '管理员' || (form['examine_state'] && $check_examine()) || (!form['examine_state'] && $check_examine())">
            <el-option key="未审核" label="未审核" value="未审核"></el-option>
            <el-option key="已通过" label="已通过" value="已通过"></el-option>
            <el-option key="未通过" label="未通过" value="未通过"></el-option>
          </el-select>
          <div v-else>{{form["examine_state"]}}</div>
        </el-form-item>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="审核回复" prop="examine_reply">
          <el-input id="examine_reply" v-model="form['examine_reply']" placeholder="请输入审核回复"
                    v-if="user_group === '管理员' || (form['examine_reply'] && $check_examine()) || (!form['examine_reply'] && $check_examine())"></el-input>
          <div v-else>{{form["examine_reply"]}}</div>
        </el-form-item>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
        <el-form-item>
          <el-button type="primary" @click="submit()">提交</el-button>
          <el-button @click="cancel()">取消</el-button>
        </el-form-item>
      </el-col>

    </el-form>
  </el-main>
</template>

<script>
import mixin from "@/mixins/page.js";

export default {
  mixins: [mixin],
  data() {
    return {
      field: "approval_documents_id",
      url_add: "~/api/approval_documents/add?",
      url_set: "~/api/approval_documents/set?",
      url_get_obj: "~/api/approval_documents/get_obj?",
      // 文件上传
      url_upload: "~/api/approval_documents/upload?",

      query: {
        "approval_documents_id": 0,
      },

      form: {
        "user": 0, // 用户
        "manager": 0, // 经理
        "file_name":'', // 文件名称
        "file_type":'', // 文件类型
        "treatment_method":'', // 处理方式
        "document_attachment":'', // 文件附件
        "document_details":'', // 文件详情
        "examine_state": "未审核",
        "examine_reply": "",
        "approval_documents_id": 0, // ID

      },
      disabledObj:{
        "user_isDisabled": false,
        "manager_isDisabled": false,
        "file_name_isDisabled": false,
        "file_type_isDisabled": false,
        "treatment_method_isDisabled": false,
        "document_attachment_isDisabled": false,
        "document_details_isDisabled": false,
      },
      // 用户列表
      list_user_user: [],
      // 用户组
      group_user_user: "",
      // 用户列表
      list_user_manager: [],
      //处理方式选项列表
      list_treatment_method: ['会签','签署'],
    }
  },
  methods: {
    /**
     * 获取普通用户用户列表
     */
    async get_list_user_user() {
      var json = await this.$get("~/api/user/get_list?user_group=普通用户");
      if(json.result && json.result.list){
        this.list_user_user = json.result.list;
      }
      else if(json.error){
        console.error(json.error);
      }
    },
    /**
     * 获取普通用户用户组
     */
    async get_group_user_user() {
      this.form["user"] = this.user.user_id;
      var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
      if(json.result && json.result.obj){
        this.group_user_user = json.result.obj;
      }
      else if(json.error){
        console.error(json.error);
      }
    },
    get_user_session_user(id){
      var _this = this;
      var user_id = {"user_id":id}
      var url = "~/api/"+_this.group_user_user.source_table+"/get_obj?"
      this.$get(url, user_id, function(res) {
        if (res.result && res.result.obj) {
          var arr = []
          for (let key in res.result.obj) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in _this.form) {
            arrForm.push(key)
          }
          for (var i=0;i<arr.length;i++){
            if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arr[i] === arrForm[j]) {
                  if (arr[i] !== "user") {
                    _this.form[arrForm[j]] = res.result.obj[arr[i]]
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                    break;
                  } else {
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  }
                }
              }
            }
          }
        }
      });
    },
    get_user_user(id){
      var obj = this.list_user_user.getObj({"user_id":id});
      var ret = "";
      if(obj){
        if(obj.nickname){
          ret = obj.nickname;}
        else{
          ret = obj.username;
        }
      }
      return ret;
    },
    /**
     * 获取经理用户用户列表
     */
    async get_list_user_manager() {
      var json = await this.$get("~/api/user/get_list?user_group=经理用户");
      if(json.result && json.result.list){
        this.list_user_manager = json.result.list;
      }
      else if(json.error){
        console.error(json.error);
      }
    },
    get_user_manager(id){
      var obj = this.list_user_manager.getObj({"user_id":id});
      var ret = "";
      if(obj){
        if(obj.nickname){
          ret = obj.nickname;}
        else{
          ret = obj.username;
        }
      }
      return ret;
    },
    /**
     * 上传文件附件
     * @param {Object} param文件参数
     */
    upload_document_attachment(param){
      this.uploadFile(param.file, "document_attachment");
    },

    /**
     * 获取对象之前
     * @param {Object} param
     */
    get_obj_before(param) {
      var form = "";
      /**
       * 请求列表前
       * @param {Object} param
       */
      var user_group = this.user.user_group;
      if (user_group !== "管理员") {
        switch (user_group) {
          case "用户":
            if(param["user"] > 0){
              param["user"] = this.user.user_id;
            }
            break;
          case "经理":
            if(param["manager"] > 0){
              param["manager"] = this.user.user_id;
            }
            break;
        }
      }
      if(this.form && form){
        Object.keys(this.form).forEach(key => {
          Object.keys(form).forEach(dbKey => {
            if(key === dbKey){
              this.disabledObj[key+'_isDisabled'] = true;
            }
          })
        })
      }
      $.db.del("form");
      return param;
    },

    /**
     * 获取对象之后
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func){
    },

    is_view(){
      var bl = this.user_group == "管理员";

      if(!bl){
        bl = this.$check_action('/approval_documents/table','add');
        console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
      }
      if(!bl){
        bl = this.$check_action('/approval_documents/table','set');
        console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
      }
      if(!bl){
        bl = this.$check_action('/approval_documents/view','add');
        console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
      }
      if(!bl){
        bl = this.$check_action('/approval_documents/view','set');
        console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
      }
      if(!bl){
        bl = this.$check_action('/approval_documents/view','get');
        console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
      }

      console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

      return bl;
    },
    /**
     * 上传文件
     * @param {Object} param
     */
    uploadimg(param) {
      this.uploadFile(param.file, "avatar");
    },

  },
  created() {
    this.get_list_user_user();
    this.get_group_user_user();
    this.get_list_user_manager();
  },
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
